package com.hxzon.netty.statistics.client;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.apache.tapestry5.json.JSONObject;
import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelFutureListener;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StatisticsClient {
    private static final Logger logger = LoggerFactory.getLogger(StatisticsClient.class);

    public static final String host = "127.0.0.1";//
    public static final int port = 8080;

    public static void sendMessage(String host, int port, final String messageSend) {
        ClientBootstrap bootstrap = new ClientBootstrap(new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));
        bootstrap.setPipelineFactory(new StatisticsClientPipelineFactory());
        ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port));
        future.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                Channel channel = future.awaitUninterruptibly().getChannel();
                logger.debug("channel id:" + channel.getId());
                channel.write(messageSend);
                channel.close();
            }
        });
        //end add
        // Wait until the connection is closed or the connection attempt fails.
        future.getChannel().getCloseFuture().awaitUninterruptibly();
        // Shut down thread pools to exit.
        bootstrap.releaseExternalResources();
    }

    public static void main(String[] args) throws Exception {
        Runnable runnable = new Runnable() {
            public void run() {
                sendMessage(host, port, loginJson());
                sendMessage(host, port, logoutJson());
                sendMessage(host, port, flowJson());
            }
        };
        for (int i = 0; i < 5; i++) {
            new Thread(runnable).start();
        }
    }

    private static String loginJson() {
        JSONObject json = new JSONObject();
        commonJson(json);
        json.put("messageType", "1");
        json.put("loginUuid", "uuid-1");
        return json.toString();
    }

    private static String logoutJson() {
        JSONObject json = new JSONObject();
        commonJson(json);
        json.put("messageType", "2");
        json.put("loginUuid", "uuid-1");
        return json.toString();
    }

    private static String flowJson() {
        JSONObject json = new JSONObject();
        commonJson(json);
        json.put("messageType", "4");
        json.put("usageValue", 33);
        return json.toString();
    }

    private static void commonJson(JSONObject json) {
        json.put("clientVersion", "12.6.3");
    }

}
